package com.atguigu.cloud.order.service.Impl;


import com.alibaba.fastjson.JSON;

import com.atguigu.cloud.order.entity.Order;
import com.atguigu.cloud.order.mapper.OrderMapper;
import com.atguigu.cloud.order.service.OrderService;
import com.atguigu.cloud.order.vo.OrderVo;
import com.atguigu.cloud.user.client.UserClient;
import com.atguigu.cloud.user.entity.User;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;

/**
 * @author LT
 * @create 2023-11-13-18:22
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    UserClient userClient;

    @Override
    public Order findOrderByOrderId(Long Id) {
        Order order = orderMapper.findOrderByOrderId(Id);
        User user = userClient.findById(order.getUserId());
        OrderVo orderVo = new OrderVo();
        BeanUtils.copyProperties(order,orderVo);
        orderVo.setUser(user);
        return orderVo;
    }
    private User getUserByRestTemplateAndLoadBalancer(Long userId){
                User user = restTemplate.getForObject
                        ("http://localhost:10100/api/user/findById/{userId}",
                User.class,userId);
                return user;
    }
    int count = 0;

    private User getUserByRestTemplateAndDiscoveryClient(Long userId){
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-user");
        int index = (count++)%instances.size();
        ServiceInstance serviceInstance = instances.get(index);
        User user = restTemplate.getForObject(
                "http://{host}:{port}/api/user/findById/{userId}",//目标服务器地址
                User.class,//查询的响应体要转换的类型
                serviceInstance.getHost() , serviceInstance.getPort(),userId);//地址的占位符需要填充的参数列表
        return user;
    }
    private User getUserByHttpClient(Long userId){
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://localhost:10100/api/user/findById/"+userId);
            CloseableHttpResponse response = httpClient.execute(httpGet);

            //获取响应体的json转为User对象使用
            String resultJson = EntityUtils.toString(response.getEntity(), "UTF-8");

            User user = JSON.parseObject(resultJson, User.class);
            return user;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
